package com.smzd.ccms.business;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smzd.ccms.dao.SiteDao;
import com.smzd.ccms.domain.Site;
import com.smzd.core.dao.IDao;
import com.smzd.framework.core.business.AbstractFGService;
import com.smzd.framework.system.dao.MeetingDao;
import com.smzd.framework.system.dao.UserDao;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.system.domain.User;
import com.smzd.framework.utils.db.FGRowConverter;

@Service
public class SiteService extends AbstractFGService<Site> {
    @Autowired
    private SiteDao dao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private MeetingDao meetingDao;
    
    @Override
    protected IDao<Site> getDao() {
        return dao;
    }
    
    @Override
    public FGRowConverter<Site> getConverter() {
        return converter;
    }

    private static FGRowConverter<Site> converter = new FGRowConverter<Site>() {
        @Override
        public Object[] convert(Site elem) {
            Object[] fs = new Object[]{
                    elem.getId(),
                    elem.getName(),
                    elem.getDescription(),
                    elem.getDomain(),
                    elem.getIcp()
                };
                return fs;
        }
    };

	public Site getSiteByMeetingId(int meetingId){
		return dao.getSiteByOwnerId(meetingId);
	}

	public Site getSiteByPath(String siteStr) {
		return dao.getSiteByPath(siteStr);
	}

	@Transactional
	public void createANewSite(Site elem, Meeting anchor, String holderName) {
		User user = new User();
    	user.setName(holderName);
    	user.setPassword("1234566");
    	user.setType(User.USER_TYPE_MEETING_OWNER);
    	user.setOwner(anchor);
    	userDao.insert(user);
    	Meeting owner = new Meeting();
    	owner.setOwner(user);
    	owner.setName(elem.getName());
    	owner.setStartTime(new Date());
    	owner.setDescription(elem.getDescription());
    	owner.setPlace(elem.getPath());
    	meetingDao.insert(owner);
    	elem.setOwner(owner);
    	dao.insert(elem);
	}
    
}